package com.gtw.modules.api.controller;

import java.util.List;
import java.util.Map;

import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbAnnouncementService;
import com.gtw.modules.entity.TbAnnouncement;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 公告表
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-06 12:14:34
 */
@RestController
@RequestMapping("/api/announcement")
public class TbAnnouncementController {
    @Autowired
    private TbAnnouncementService tbAnnouncementService;

    /**
     * 列表
     */
    @AuthIgnoreApi
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TbAnnouncement> announcementList = tbAnnouncementService.queryList(query);
        int total = tbAnnouncementService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(announcementList, total, query.getLimit(), query.getPage());

        return R.ok().put("data", pageUtil);
    }


    /**
     * 信息
     */
    @AuthIgnoreApi
    @RequestMapping("/info")
    public R info(Integer id) {
        TbAnnouncement announcement = tbAnnouncementService.queryObject(id);

        return R.ok().put("data", announcement);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TbAnnouncement announcement) {
        tbAnnouncementService.save(announcement);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbAnnouncement announcement) {
        tbAnnouncementService.update(announcement);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tbAnnouncementService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查询所有数组
     */
    @RequestMapping("/all")
    public R all() {
        List<TbAnnouncement> tbAnnouncementsList = tbAnnouncementService.findALL();
        int[] ids = new int[tbAnnouncementsList.size()];
        for (int i = 0; i < tbAnnouncementsList.size(); i++) {
            ids[i] = tbAnnouncementsList.get(i).getId();
        }
        return R.ok().put("data", ids);
    }

}
